<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <input id="bookname" type="text">
    <button>添加</button>
    <ul></ul>

    <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
    <script>
        // const url = 'http://www.liulongbin.top:3006/api/getbooks';
        // axios.get(url);

        const inp = document.querySelector('#bookname');
        const btn = document.querySelector('button');

        const url = 'http://www.liulongbin.top:3006/api/addbook';

        btn.addEventListener('click', () => {
            // 添加
            axios({
                method: 'post',
                url,
                data: {
                    bookname: inp.value,
                    author: 'yuzelin',
                    publisher: '人民日报',
                },
            }).then((res) => {

                alert(res.data.msg);

                console.log(res);

                if (res.data.status === 502) {
                    return;
                }

                axios({
                    method: 'get',
                    url: 'http://www.liulongbin.top:3006/api/getbooks',
                }).then((res) => {
                    console.log(res.data.data);
                    const ul = document.querySelector('ul');
                    ul.innerHTML = '';
                    for (const item of res.data.data) {
                        const li = document.createElement('li');
                        li.innerText = item.bookname;
                        ul.appendChild(li);
                    }
                });

            });

        });

    </script>
</body>
</html>